//
//  CYABDetailTableVC.m
//  CreditAddressBook
//
//  Created by Lee on 15/10/12.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYABDetailTableVC.h"
#import "LESegmentView.h"
#import "CYPerson.h"
#import "CYCallRecords.h"
#import "CYABPhoneVC.h"
#import "CYTool.h"
#import "CYDialTool.h"
#import "CYCallRecordsTableVC.h"
#import "CYStatisticsContentVC.h"
#import "CYStatisticsTopView.h"
#import "CYDatabase.h"

@interface CYABDetailTableVC ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *cellView;

@property (nonatomic ,strong) LESegmentView *seg;

@property (nonatomic ,assign) CGFloat rowH;

@property (nonatomic ,strong) CYABPhoneVC *phoneVC;//电话号码
@property (nonatomic ,strong) CYCallRecordsTableVC *callRecordsVC;//通话记录
@property (nonatomic ,strong) CYStatisticsContentVC *statisVC;
@property (nonatomic ,strong) UIViewController *currentVC;//当前显示的控制器
@end

@implementation CYABDetailTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.decelerationRate = 0;
//    self.tableView.bounces = NO;
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
//    self.rowH = 2000;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self setupSegmentView];
    
    [self setupSubController];
}

- (void)setupSegmentView {
    LESegmentView *seg = [[LESegmentView alloc] init];
    seg.width = CYScreeSize.width;
    seg.height = 50;
    self.seg = seg;
    seg.segmentSelectedIndex = LESelectedIndexThird;
    [seg addSegmentButtonWithTitles:@[@"1", @"2", @"3"] andImageNames:@[@"ic_main2", @"ic_main2", @"ic_main2"] clickAtIndex:^(NSInteger buttonIndex) {
        if (self.currentVC) {
            switch (buttonIndex) {
                case CYIndexFirst:
                    [self replaceController:self.currentVC newController:self.statisVC];
                    break;
                    
                case CYIndexSecond:
                    [self replaceController:self.currentVC newController:self.callRecordsVC];
                    break;
                    
                case CYIndexThird:
                    [self replaceController:self.currentVC newController:self.phoneVC];
                    break;
            }
        }
    }];
}

- (void)setupSubController {
    //电话详情控制器
    CYABPhoneVC *phoneVC = [[CYABPhoneVC alloc] init];
    phoneVC.person = self.person;
    self.phoneVC = phoneVC;
    self.currentVC = phoneVC;
    [self addChildViewController:phoneVC];
    phoneVC.view.y = 10;
    //计算滚动的高度
//    self.rowH = phoneVC.phoneVCHeight;
    [self.cellView addSubview:phoneVC.view];
    [phoneVC didMoveToParentViewController:self];
    
//    [self.bottomContentIView layoutIfNeeded];
//    self.bottomContentIView.height = phoneVC.phoneVCHeight;
//    [self.bottomContentIView addSubview:phoneVC.view];
//    
    //通话记录
//    CYCallRecords *calls = [CYCallRecords loadFromNibUsingClassName];
//    calls.callRecords = [self loadCallHistory:self.person.phones];
//    calls.scrollEnabled = NO;
//    UIViewController *callRecordsVC = [[UIViewController alloc] init];
//    callRecordsVC.view = calls;
//    callRecordsVC.view.height = calls.viewH;
    //    self.callRecordsVC = callRecordsVC;
    
    CYCallRecordsTableVC *callRecords = [[CYCallRecordsTableVC alloc] init];
    callRecords.callRecords = [self loadCallHistory:self.person.phones];
    self.callRecordsVC = callRecords;
    self.rowH = callRecords.viewH;

    //统计
    CYStatisticsContentVC *statiVC = [[CYStatisticsContentVC alloc] init];
    statiVC.person = self.person;
    self.statisVC = statiVC;
    CYStatisticsTopView *topview = [CYStatisticsTopView topView];
    statiVC.topView = topview;
    statiVC.view.height = CYScreeSize.height - 50 - 260;
    statiVC.view.width = CYScreeSize.width;
//    statiVC.view.frame = self.bottomContentIView.bounds;
    
    //失信人详情
    
}

#pragma mark - Table view data source

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @" ";
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.seg;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat rowH = self.rowH;
    CYLog(@"rowH:%f", rowH);
    return rowH;
}

#pragma mark - scrollview代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    CYLog(@"a:%f", scrollView.contentOffset.y);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    if (scrollView.contentOffset.y < 0) {
//        [scrollView setContentOffset:CGPointMake(0, -64) animated:YES];
//    } else if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y < 65) {
//        [scrollView setContentOffset:CGPointMake(0, 196) animated:YES];
//    } else if (scrollView.contentOffset.y >= 65 && scrollView.contentOffset.y < 130) {
//        [scrollView setContentOffset:CGPointMake(0, -64) animated:YES];
//    } else if (scrollView.contentOffset.y >= 130 && scrollView.contentOffset.y < 196) {
//        [scrollView setContentOffset:CGPointMake(0, 196) animated:YES];
//    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//    if (scrollView.contentOffset.y < 0) {
//        [scrollView setContentOffset:CGPointMake(0, -64) animated:YES];
//    } else if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y < 65) {
//        [scrollView setContentOffset:CGPointMake(0, 196) animated:YES];
//    } else if (scrollView.contentOffset.y >= 65 && scrollView.contentOffset.y < 130) {
//        [scrollView setContentOffset:CGPointMake(0, -64) animated:YES];
//    } else if (scrollView.contentOffset.y >= 130 && scrollView.contentOffset.y < 196) {
//        [scrollView setContentOffset:CGPointMake(0, 196) animated:YES];
//    }
}

#pragma mark - 多控制器切换
/**
 *	切换各个标签内容
 *  transitionFromViewController:toViewController:duration:options:animations:completion:
 *  fromViewController	  当前显示在父视图控制器中的子视图控制器
 *  toViewController		将要显示的姿势图控制器
 *  duration				动画时间(这个属性,old friend 了 O(∩_∩)O)
 *  options				 动画效果(渐变,从下往上等等,具体查看API)
 *  animations			  转换过程中得动画
 *  completion			  转换完成
 */
- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController {
    [self addChildViewController:newController];
    [self transitionFromViewController:oldController toViewController:newController duration:0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) {
        if (finished) {
            [newController didMoveToParentViewController:self];
            [oldController willMoveToParentViewController:nil];
            [oldController removeFromParentViewController];
            self.currentVC = newController;
        }else{
            self.currentVC = oldController;
        }
    }];
}

#pragma mark - 数据库操作
/**
 *  加载通话记录
 */
- (NSArray *)loadCallHistory:(NSArray *)phones {
    NSMutableArray *calls = [NSMutableArray array];
    for (NSString *phone in phones) {
        NSArray *arr = [[CYDatabase sharedDatabase] callHistorysWithParam:@{@"phone": phone}];
        if (arr && arr.count) {
            [calls addObjectsFromArray:arr];
        }
    }
    return calls;
}

#pragma mark - 
- (CGFloat)rowHeight {
    if ([self.currentVC isKindOfClass:[CYCallRecordsTableVC class]]) {
        return self.callRecordsVC.viewH + 10;
    } else if ([self.currentVC isKindOfClass:[CYStatisticsContentVC class]]) {
        return CYScreeSize.height - 50 - 260 + 10;
    } else {
        return self.phoneVC.phoneVCHeight + 10;
    }
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

@end
